package com.hellojavaweb.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import java.io.IOException;

/**
 * @模板Servlet类
 * @Servlet类的作用：接收浏览器访问请求，处理，最后返回响应的内容
 * @example： SayHelloServlet 是用于浏览访问网址 http://xxxx:8080/say?says=hello，服务器返回"Welcome, you said: hello"
 * Created by l00383533 on 2017/1/10.
 */
public class SayHelloServlet extends javax.servlet.http.HttpServlet {

    /**
     * 该方法下接收的是POST请求的参数 在form表单中表现为 method="POST"
     *
     * @param request
     * @param response
     */
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        //1.获取浏览器传入的参数
        String sayWords = request.getParameter("says");

        //2.校验参数的正确性
        if (sayWords == null || sayWords.equals("")) {
            response.getWriter().append("参数错误 Param wrong.");
            return;
        }

        //3.将参数值存储在数据库中
        //.....略

        //4.返回内容
//存放数据
        ServletContext application = request.getServletContext();
        application.setAttribute("words","hello world");
//页面跳转
        RequestDispatcher dispatcher = request.getRequestDispatcher("say.jsp");
        dispatcher.forward(request,response);        
        
          }

    /**
     * 该方法下接收的是GET请求的参数 在form表单中表现为 method="GET"
     *
     * @param request
     * @param response
     */
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doPost(request, response);
    }
}
